package org.example.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.springcloud.entities.Payment;
import org.example.springcloud.result.CommonResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * Created with IntelliJ IDEA.
 *
 * @author Jandmla
 * Date: 2022/7/22
 * Time: 15:34
 * Description:
 */
@RestController
@Slf4j
public class OrderControllerImpl implements OrderController{

    /**
     * 单机版可以直接指定URL，但是集群是需要动态改变，因此不使用
     */
    @Deprecated
    public static final String PATH_URL = "http://localhost:8001";

    /**
     * Eureka 中的服务名称都是大写，但是在Zookeeper中都是小写
     */
    @Deprecated
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

    public static final String ZK_URL = "http://cloud-payment-service";
    private final RestTemplate restTemplate;

    public OrderControllerImpl(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @Override
    @PostMapping(value = "consumer/payment/create")
    public CommonResult create(@RequestBody Payment payment) {
        return restTemplate.postForObject(ZK_URL+"/payment/create",payment,CommonResult.class);
    }

    @Override
    @GetMapping(value = "consumer/payment/get/{id}")
    public CommonResult getPayment(@PathVariable("id") Long id) {
        return restTemplate.getForObject(ZK_URL+"/payment/get/"+id,CommonResult.class);
    }
}